package com.agile.major.web.rest;

import com.agile.major.domain.BaseDictionaries;
import com.agile.major.service.BaseDictionariesService;
import com.agile.major.web.rest.util.HeaderUtil;
import com.agile.major.web.rest.util.PaginationUtil;
import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing BaseDictionaries.
 */
@RestController
@RequestMapping("/api")
public class BaseDictionariesResource {

    private final Logger log = LoggerFactory.getLogger(BaseDictionariesResource.class);

    private static final String ENTITY_NAME = "baseDictionaries";

    private final BaseDictionariesService baseDictionariesService;

    public BaseDictionariesResource(BaseDictionariesService baseDictionariesService) {
        this.baseDictionariesService = baseDictionariesService;
    }

    /**
     * POST  /base-dictionaries : Create a new baseDictionaries.
     *
     * @param baseDictionaries the baseDictionaries to create
     * @return the ResponseEntity with status 201 (Created) and with body the new baseDictionaries, or with status 400 (Bad Request) if the baseDictionaries has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/base-dictionaries")
    @Timed
    public ResponseEntity<BaseDictionaries> createBaseDictionaries(@RequestBody BaseDictionaries baseDictionaries) throws URISyntaxException {
        log.debug("REST request to save BaseDictionaries : {}", baseDictionaries);
        if (baseDictionaries.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new baseDictionaries cannot already have an ID")).body(null);
        }
        BaseDictionaries result = baseDictionariesService.save(baseDictionaries);
        return ResponseEntity.created(new URI("/api/base-dictionaries/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /base-dictionaries : Updates an existing baseDictionaries.
     *
     * @param baseDictionaries the baseDictionaries to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated baseDictionaries,
     * or with status 400 (Bad Request) if the baseDictionaries is not valid,
     * or with status 500 (Internal Server Error) if the baseDictionaries couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/base-dictionaries")
    @Timed
    public ResponseEntity<BaseDictionaries> updateBaseDictionaries(@RequestBody BaseDictionaries baseDictionaries) throws URISyntaxException {
        log.debug("REST request to update BaseDictionaries : {}", baseDictionaries);
        if (baseDictionaries.getId() == null) {
            return createBaseDictionaries(baseDictionaries);
        }
        BaseDictionaries result = baseDictionariesService.save(baseDictionaries);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, baseDictionaries.getId().toString()))
            .body(result);
    }

    /**
     * GET  /base-dictionaries : get all the baseDictionaries.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of baseDictionaries in body
     */
    @GetMapping("/base-dictionaries")
    @Timed
    public ResponseEntity<Page<BaseDictionaries>> getAllBaseDictionaries(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of BaseDictionaries");
        Page<BaseDictionaries> page = baseDictionariesService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/base-dictionaries");
        return new ResponseEntity<>(page, headers, HttpStatus.OK);
    }

    /**
     * GET  /base-dictionaries/:id : get the "id" baseDictionaries.
     *
     * @param id the id of the baseDictionaries to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the baseDictionaries, or with status 404 (Not Found)
     */
    @GetMapping("/base-dictionaries/{id}")
    @Timed
    public ResponseEntity<BaseDictionaries> getBaseDictionaries(@PathVariable Long id) {
        log.debug("REST request to get BaseDictionaries : {}", id);
        BaseDictionaries baseDictionaries = baseDictionariesService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(baseDictionaries));
    }

    /**
     * DELETE  /base-dictionaries/:id : delete the "id" baseDictionaries.
     *
     * @param id the id of the baseDictionaries to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/base-dictionaries/{id}")
    @Timed
    public ResponseEntity<Void> deleteBaseDictionaries(@PathVariable Long id) {
        log.debug("REST request to delete BaseDictionaries : {}", id);
        baseDictionariesService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    @GetMapping("/base-dictionaries/type")
    @Timed
    public ResponseEntity<List<BaseDictionaries>> getBaseDictionariesByType(@RequestParam String type) {
        log.debug("REST request to  getBaseDictionariesByType : {}", type);
        List<BaseDictionaries> baseDictionaries = baseDictionariesService.findByType(type);
        return ResponseEntity.ok(baseDictionaries);
    }
}
